package com.next.aappublicapp;

import org.apache.http.ParseException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.util.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.next.aappublicapp.util.JsonUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class FullFacebookPostContentActivity extends Activity {

	public static final String POST_JSON_OBJECT = "POST_JSON_OBJECT";
	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {

			setContentView(R.layout.activity_full_facebook_post);

			Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
			String postJsonString = bundle.getString(POST_JSON_OBJECT);
			JSONObject postJson = new JSONObject(postJsonString);
			Log.i("Ravi", "imageUrl=" + postJson);

			//ImageView imageView = (ImageView) findViewById(R.id.postImage);
			ImageView postContentImage = (ImageView) findViewById(R.id.postContentImage);
			TextView userName = (TextView) findViewById(R.id.userName);
			TextView postTime = (TextView) findViewById(R.id.postTime);
			TextView postContent = (TextView) findViewById(R.id.postContent);

			userName.setText("Aam Aadmi Party");
			postTime.setText(postJson.getString("created_time"));
			String postMessage = postJson.getString("message");
			postContent.setText(postMessage);

			String imageUrl = JsonUtil.getSringFromJsonObject(postJson, "picture");
			if (imageUrl == null || imageUrl.trim().isEmpty()) {
				postContentImage.setVisibility(View.GONE);
			} else {
				postContentImage.setVisibility(View.VISIBLE);
				final String finalImageUrl = imageUrl = StringUtils.replace(
						imageUrl, "_s.", "_b.");
				// new DownloadImageTask(postContentImage).execute(imageUrl);
				ImageLoader.getInstance().displayImage(finalImageUrl,
						postContentImage);

				postContentImage.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						Intent intent = new Intent(
								FullFacebookPostContentActivity.this,
								FullImageActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString(FullImageActivity.IMAGE_URL,
								finalImageUrl);
						intent.putExtra(FullImageActivity.ARGS_BUNDLE, bundle);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					}
				});
				// downloadImageTaskParams.add(new
				// DownloadImageTaskParam(postContentImage, imageUrl));
			}

		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

}
